import java.util.Arrays;
import java.util.Scanner;

public class UserManagementSystem {
    private static String[] usernames = new String[10];
    private static String[] passwords = new String[10];
    private static int userCount = 0;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("_________管理系统_________");
            System.out.println("请选择操作：");
            System.out.println("1. 登录功能");
            System.out.println("2. 注册功能");
            System.out.println("3. 查看所有用户");
            System.out.println("4. 退出");
            System.out.println("_________管理系统_________");

            int choice = scanner.nextInt();
            scanner.nextLine();  // 消耗掉输入缓冲区的换行符

            switch (choice) {
                case 1:
                    login(scanner);
                    break;
                case 2:
                    register(scanner);
                    break;
                case 3:
                    viewUsers();
                    break;
                case 4:
                    System.out.println("已退出系统。");
                    System.exit(0);
                default:
                    System.out.println("无效的选择，请重新输入。");
            }
        }
    }

    private static void login(Scanner scanner) {
        System.out.print("请输入用户名：");
        String username = scanner.nextLine();
        System.out.print("请输入密码：");
        String password = scanner.nextLine();

        int index = findUser(username);
        if (index != -1 && passwords[index].equals(password)) {
            System.out.println("登录成功！");
        } else {
            System.out.println("用户名或密码错误，登录失败。");
        }
    }

    private static void register(Scanner scanner) {
        if (userCount == usernames.length) {
            // 扩展数组大小
            usernames = Arrays.copyOf(usernames, usernames.length * 2);
            passwords = Arrays.copyOf(passwords, passwords.length * 2);
        }

        System.out.print("请输入新用户名：");
        String newUsername = scanner.nextLine();
        System.out.print("请输入新密码：");
        String newPassword = scanner.nextLine();

        if (findUser(newUsername) == -1) {
            usernames[userCount] = newUsername;
            passwords[userCount] = newPassword;
            userCount++;
            System.out.println("注册成功！");
        } else {
            System.out.println("用户名已存在，注册失败。");
        }
    }

    private static void viewUsers() {
        System.out.println("所有用户信息：");
        for (int i = 0; i < userCount; i++) {
            System.out.println("用户名：" + usernames[i] + "，密码：" + passwords[i]);
        }
    }

    private static int findUser(String username) {
        for (int i = 0; i < userCount; i++) {
            if (usernames[i].equals(username)) {
                return i;
            }
        }
        return -1;
    }
}
